/*
 * jxUtils Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxUtils.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;

import org.jxUtils.check.ParameterCheck;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxUtils
 * @package org.jxUtils.collection
 * @date 10/02/2014 - 20:46:33
 * @param <T>
 */
public class ListSet<T> extends ArrayList<T> implements Set<T> {
	
	private static final long serialVersionUID = 3625876717501805100L;
	
	/**
	 * 
	 */
	public ListSet() {
	
		super();
	}
	
	/**
	 * @param collection
	 */
	public ListSet(final Collection<? extends T> collection) {
	
		super(collection);
	}
	
	/**
	 * @param initialCapacity
	 */
	public ListSet(final int initialCapacity) {
	
		super(initialCapacity);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.ArrayList#add(int, java.lang.Object)
	 */
	@Override
	public void add(final int index, final T element) {
	
		//
		if( !contains(element)) {
			
			super.add(index, element);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.ArrayList#add(java.lang.Object)
	 */
	@Override
	public boolean add(final T element) {
	
		//
		if(contains(element)) {
			
			return false;
		}
		
		return super.add(element);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.ArrayList#addAll(java.util.Collection)
	 */
	@Override
	public boolean addAll(final Collection<? extends T> collection) {
	
		ParameterCheck.isNull(collection, "collection");
		
		//
		if(containsAny(collection)) {
			
			return false;
		}
		
		return super.addAll(collection);
	}
	
	/**
	 * @param collection
	 * @return contains any
	 */
	public boolean containsAny(final Collection<? extends T> collection) {
	
		ParameterCheck.isNull(collection, "collection");
		
		//
		for(final T element : collection) {
			
			//
			if(contains(element)) {
				
				return true;
			}
		}
		
		return false;
	}
}
